using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace GameWP
{
    public class MainGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        DBCharacter dbCharacter;
        DBFont dbFont;
        DBTexture dbTexture;
        public List<GameScreen> ListGameScreen;
        LoadingComponent loadingComponent;

        public MainGame()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            
            graphics.IsFullScreen = true;
            graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;

            TouchPanel.EnabledGestures =   GestureType.Tap |   GestureType.DoubleTap | GestureType.Hold;

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);          
        }

        protected override void Initialize()
        {

            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Load from Content
            ManagerGame.managerSaveLoad = new DBSaveGame();
            dbCharacter = new DBCharacter(Content, graphics);
            dbTexture = new DBTexture(Content, graphics);
            dbFont = new DBFont(Content, graphics);            
            ManagerGame.luffy = dbCharacter.luffy;
            ManagerGame.managerAHero = new ManagerAHero(dbCharacter.AHeros, dbCharacter.ListIconAHeros, dbFont.FontScore);
            ManagerGame.managerZombie = new ManagerZombie(dbCharacter.Zombies, dbFont.FontMenuMini);
            ManagerGame.managerTrap = new ManagerTrap(dbCharacter.ListTraps, dbCharacter.ListIconInShop, dbTexture.IconUp, dbTexture.IconDown, dbFont.FontMenuMini);
            ManagerGame.managerSound = new DBSound(Content, graphics);
            ManagerGame.treasure = dbCharacter.treasure;
            ManagerGame.goToShop = new Button(dbTexture.ViewIcons[4], new Vector2(120, 70));
            ManagerGame.camera = new Camera(GraphicsDevice.Viewport) { Limits = new Rectangle(0, 0, 2048, 480) };
            ManagerGame.AppearAura = new Aura(dbTexture.listAppearAura, new Vector2(0, 230));
            loadingComponent = new LoadingComponent(this, dbTexture.listLoading, dbTexture.listGuide, dbTexture.listBG[8], dbFont.FontMenu, dbFont.FontArial, spriteBatch);

            //Init Gamescreen
            ListGameScreen = new List<GameScreen>();
                // 00 Tao MenuScreen
                MenuScreen menuScreen = new MenuScreen(this, this, spriteBatch, dbTexture.listBG, dbFont.FontMenu);
                ListGameScreen.Add(menuScreen);
                // 01 Tao SettingScreen
                SettingScreen settingScreen = new SettingScreen(this, this, spriteBatch, dbTexture.listBG[3], dbFont.FontMenuMini, dbFont.FontArial);
                ListGameScreen.Add(settingScreen);
                // 02 Tao AboutScreen
                AboutScreen aboutScreen = new AboutScreen(this, this, spriteBatch, dbTexture.listBG[4], dbFont.FontMenuMini, dbFont.FontArial);
                ListGameScreen.Add(aboutScreen);
                // 03 Tao Upgrade Screen theo thong so Luffy
                UpgradeScreen upgradeScreen = new UpgradeScreen(this, this, spriteBatch, dbTexture.listBG[5], dbFont.FontMenu, dbFont.FontArial, dbFont.FontArialMini, dbTexture.UpLevel, dbCharacter.ListIconAHeros);
                ListGameScreen.Add(upgradeScreen);
                // 04 Tao Member Screen theo thong so Luffy (Luffy da mo duoc cac member nao )
                MemberScreen memberScreen = new MemberScreen(this, this, spriteBatch, dbTexture.listBG[6], dbTexture.StartGame, dbFont.FontMenu, dbFont.FontArial, dbCharacter.ListIconAHeros);
                ListGameScreen.Add(memberScreen);
                // 05 Tao Play Screen
                PlayScreen playScreen = new PlayScreen(this, this, spriteBatch, dbFont.FontMenuMini, dbFont.FontMenu, dbTexture.listBG, dbTexture.ViewIcons, dbTexture.listLayerBehind,dbTexture.listLayerFront);
                ListGameScreen.Add(playScreen);

            //Tao event chuyen screen + Add cac gameScreen vao ComponentGame;
            for (int i = 0; i < ListGameScreen.Count; i++)
            {
                ListGameScreen[i].Showed += new GameScreen.IsShowed(ActiveScreen);
                Components.Add(ListGameScreen[i]);
            }

            // MenuScreen start
            ListGameScreen[0].Show(0);
        }

        //Function bat su kien show cua mot gamescreen ~ cac gamescreen con lai phai hide
        void ActiveScreen(int index)
        {
            for (int i = 0; i < ListGameScreen.Count; i++)
                if (i != index)
                    ListGameScreen[i].Hide();
            if (index == 3) ListGameScreen[index].ResetScreen(0);
            if (index == 5 ) ListGameScreen[index].ResetScreen(ManagerGame.luffy.LevelScreen);
        }

        public void ShowScreen(int index)
        {
            if (index == 3 || index == 5)
                loadingComponent.CallIsShow();
            ListGameScreen[index].Show(index);

        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            if (loadingComponent.IsShow)
            {
                loadingComponent.Update(gameTime);
            }
            else base.Update(gameTime);
            
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            if (loadingComponent.IsShow)
            {
                loadingComponent.Draw(gameTime);
            }else base.Draw(gameTime);
        }
    }
}